package controller;

import bean.Emp;
import bean.ResultCode;
import com.alibaba.fastjson.JSONArray;
import service.EmpService;
import service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/emp")
public class EmpController extends BaseController {
    private EmpService empService;

    @Override
    public void init() throws ServletException {
        empService = new EmpServiceImpl();
    }

    private void find(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Emp emp=new Emp();
        String ename = request.getParameter("ename");
        String jobs = request.getParameter("job");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        String[] allJob;
        System.out.println(ename);
        System.out.println(jobs);
        if (!"".equals(ename)) {
            emp.setEname("%"+ename+"%");
        }
        if (!"".equals(jobs)) {
          emp.setArray(jobs.substring(0,jobs.length()-1).split(","));
        }
        if(!("0".equals(begin)||"".equals(begin))){
            emp.setBeginSal(Double.parseDouble(begin));
        }
        if(!("0".equals(end)||"".equals(end))){
            emp.setEndSal(Double.parseDouble(end));
        }
        List<Emp> list = empService.find(emp);
        ResultCode<List<Emp>> rc = new ResultCode<>("200", null, list);
        String json = JSONArray.toJSONString(rc);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    private void job(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<String> list = empService.allJob();
        ResultCode<List<String>> rc = new ResultCode<>("200", null, list);
        String json = JSONArray.toJSONString(rc);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }
}
